package com.jzo2o.customer.controller.bank;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.customer.model.domain.BankAccount;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.service.BankAccountService;
import com.jzo2o.mvc.utils.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author itcast
 */
@RestController("BankAccountController")
@RequestMapping
@Api(tags = "服务端/机构端-银行账户相关接口")
public class BankAccountController {

    @Autowired
    private BankAccountService bankAccountService;

    /**
     * 服务端-新增或更新银行账号信息
     * @param bankAccountUpsertReqDTO
     * @return
     */
    @PostMapping("/worker/bank-account")
    @ApiOperation("新增或更新银行账号信息")
    public void addAndUpdate(@RequestBody BankAccountUpsertReqDTO bankAccountUpsertReqDTO) {
        if (ObjectUtil.isNotEmpty(bankAccountUpsertReqDTO)){
            BankAccount bankAccount = new BankAccount();
            BeanUtil.copyProperties(bankAccountUpsertReqDTO,bankAccount);
            BankAccount byId = bankAccountService.getById(UserContext.currentUserId());
            if (ObjectUtil.isNotEmpty(byId)){
                bankAccountService.update(bankAccount,null);
            }else {
                bankAccount.setId(UserContext.currentUserId());
                bankAccountService.save(bankAccount);
            }
        }else {
            throw new BadRequestException("账户信息不能为空！！");
        }
    }

    /**
     * 服务端-获取当前用户银行账号
     * @return
     */
    @GetMapping("/worker/bank-account/currentUserBankAccount")
    @ApiOperation("获取当前用户银行账号")
    public BankAccount getBankAccount() {
        BankAccount bankAccount = bankAccountService.getById(UserContext.currentUserId());
        if (ObjectUtil.isNotEmpty(bankAccount)){
            return bankAccount;
        }else {
            throw new BadRequestException("账户信息为空！！！");
        }
    }

    /**
     * 机构端-新增或更新银行账号信息
     * @param bankAccountUpsertReqDTO
     * @return
     */
    @PostMapping("/agency/bank-account")
    @ApiOperation("新增或更新银行账号信息")
    public void addAndUpdateI(@RequestBody BankAccountUpsertReqDTO bankAccountUpsertReqDTO) {
        if (ObjectUtil.isNotEmpty(bankAccountUpsertReqDTO)){
            BankAccount bankAccount = new BankAccount();
            BeanUtil.copyProperties(bankAccountUpsertReqDTO,bankAccount);
            BankAccount byId = bankAccountService.getById(UserContext.currentUserId());
            if (ObjectUtil.isNotEmpty(byId)){
                bankAccountService.update(bankAccount,null);
            }else {
                bankAccount.setId(UserContext.currentUserId());
                bankAccountService.save(bankAccount);
            }
        }else {
            throw new BadRequestException("账户信息不能为空！！");
        }
    }

    /**
     * 机构端-获取当前用户银行账号
     * @return
     */
    @GetMapping("/agency/bank-account/currentUserBankAccount")
    @ApiOperation("获取当前用户银行账号")
    public BankAccount getBankAccountI() {
        BankAccount bankAccount = bankAccountService.getById(UserContext.currentUserId());
        if (ObjectUtil.isNotEmpty(bankAccount)){
            return bankAccount;
        }else {
            throw new BadRequestException("账户信息为空！！！");
        }
    }
}
